package jnpf.zcasset.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.hutool.core.util.ObjectUtil;
import cn.xuyanwu.spring.file.storage.FileInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.model.dictionarytype.DictionaryTypeSelectModel;
import jnpf.base.model.dictionarytype.DictionaryTypeSelectVO;
import jnpf.base.vo.DownloadVO;
import jnpf.base.vo.ListVO;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.config.ConfigValueUtil;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.permission.entity.UserEntity;
import jnpf.util.*;
import jnpf.util.treeutil.SumTree;
import jnpf.util.treeutil.newtreeutil.TreeDotUtils;
import jnpf.zcasset.entity.ZcAssetAreaEntity;
import jnpf.zcasset.entity.ZcAssetImageEntity;
import jnpf.zcasset.entity.ZcAssetInfoEntity;
import jnpf.zcasset.entity.ZcAssetWarrantEntity;
import jnpf.zcasset.model.zcassetinfo.*;
import jnpf.zcasset.service.ZcAssetAreaService;
import jnpf.zcasset.service.ZcAssetImageService;
import jnpf.zcasset.service.ZcAssetInfoService;
import jnpf.zcasset.service.ZcAssetWarrantService;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资产信息
 *
 * @版本： V3.2.0
 * @版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期：
 */
@Slf4j
@RestController
@Api(tags = "资产信息", value = "zc")
@RequestMapping("/api/zc/zcAssetInfo")
public class ZcAssetInfoController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private ConfigValueUtil configValueUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private ZcAssetInfoService zcAssetInfoService;
    @Autowired
    private ZcAssetWarrantService zcAssetWarrantService;
    @Autowired
    private ZcAssetAreaService zcAssetAreaService;
    @Autowired
    private ZcAssetImageService zcAssetImageService;

    /**
     * 列表
     *
     * @param zcAssetInfoPagination
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZcAssetInfoPagination zcAssetInfoPagination) throws IOException {
        List<ZcAssetInfoEntity> list = zcAssetInfoService.getList(zcAssetInfoPagination);
        //处理id字段转名称，若无需转或者为空可删除
        List<ZcAssetInfoListVO> listVO = JsonUtil.getJsonToList(list, ZcAssetInfoListVO.class);
        for (ZcAssetInfoListVO zcAssetInfoVO : listVO) {
            zcAssetInfoVO.setAssetCode_name(zcAssetInfoVO.getAssetCode());

            zcAssetInfoVO.setAssetName_name(zcAssetInfoVO.getAssetName());

            zcAssetInfoVO.setAssetTags_name(zcAssetInfoVO.getAssetTags());

            zcAssetInfoVO.setDetailedAddress_name(zcAssetInfoVO.getDetailedAddress());

            zcAssetInfoVO.setAssetNature_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getAssetNature(), "610823452110094661"));

            zcAssetInfoVO.setAssetUse_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getAssetUse(), "610823865056100677"));

            zcAssetInfoVO.setAssetSource_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getAssetSource(), "610823965094445381"));

            zcAssetInfoVO.setPropertyRightUnit_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getPropertyRightUnit(), "610824055188095301"));

            zcAssetInfoVO.setManagementUnit_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getManagementUnit(), "610824142995849541"));

            zcAssetInfoVO.setManagerId_name(generaterSwapUtil.userSelectValues(zcAssetInfoVO.getManagerId()));

            zcAssetInfoVO.setCoverPictureUrl_name(generaterSwapUtil.getFileNameInJson(zcAssetInfoVO.getCoverPictureUrl()));

            zcAssetInfoVO.setRemark_name(zcAssetInfoVO.getRemark());

            zcAssetInfoVO.setAttachmentUrls_name(generaterSwapUtil.getFileNameInJson(zcAssetInfoVO.getAttachmentUrls()));

            ZcAssetWarrantEntity zcAssetWarrantEntity = zcAssetInfoService.getZcAssetWarrant(zcAssetInfoVO.getId());
            if (ObjectUtil.isNotEmpty(zcAssetWarrantEntity)) {
                ZcAssetWarrantListVO zcAssetWarrantListVO = JsonUtil.getJsonToBean(zcAssetWarrantEntity, ZcAssetWarrantListVO.class);
                zcAssetWarrantListVO.setType_name(generaterSwapUtil.getDicName(zcAssetWarrantListVO.getType(), "610826202277478725"));

                zcAssetWarrantListVO.setCode_name(zcAssetWarrantListVO.getCode());

                zcAssetWarrantListVO.setLandUse_name(zcAssetWarrantListVO.getLandUse());

                zcAssetWarrantListVO.setRoomUse_name(zcAssetWarrantListVO.getRoomUse());

                zcAssetWarrantListVO.setPropertyUse_name(zcAssetWarrantListVO.getPropertyUse());

                zcAssetWarrantListVO.setCertifiedAddress_name(zcAssetWarrantListVO.getCertifiedAddress());

                zcAssetWarrantListVO.setNowPropertyUse_name(zcAssetWarrantListVO.getNowPropertyUse());

                zcAssetWarrantListVO.setLandAuthorityNature_name(generaterSwapUtil.getDicName(zcAssetWarrantListVO.getLandAuthorityNature(), "610826295164535109"));

                zcAssetWarrantListVO.setNumberOblocks_name(zcAssetWarrantListVO.getNumberOblocks());

                zcAssetWarrantListVO.setGeneralFloor_name(zcAssetWarrantListVO.getGeneralFloor());

                zcAssetWarrantListVO.setFloorNum_name(zcAssetWarrantListVO.getFloorNum());

                zcAssetWarrantListVO.setStartDate_name(zcAssetWarrantListVO.getStartDate());

                zcAssetWarrantListVO.setEndDate_name(zcAssetWarrantListVO.getEndDate());

                zcAssetWarrantListVO.setIssueDate_name(zcAssetWarrantListVO.getIssueDate());

                zcAssetWarrantListVO.setCustodyPlace_name(zcAssetWarrantListVO.getCustodyPlace());

                zcAssetWarrantListVO.setRemark_name(zcAssetWarrantListVO.getRemark());

                zcAssetWarrantListVO.setAttachmentUrls_name(generaterSwapUtil.getFileNameInJson(zcAssetWarrantListVO.getAttachmentUrls()));

                zcAssetWarrantListVO.setImageUrl_name(generaterSwapUtil.getFileNameInJson(zcAssetWarrantListVO.getImageUrl()));

                zcAssetInfoVO.setZc_asset_warrant(zcAssetWarrantListVO);
            }
            ZcAssetAreaEntity zcAssetAreaEntity = zcAssetInfoService.getZcAssetArea(zcAssetInfoVO.getId());
            if (ObjectUtil.isNotEmpty(zcAssetAreaEntity)) {
                ZcAssetAreaListVO zcAssetAreaListVO = JsonUtil.getJsonToBean(zcAssetAreaEntity, ZcAssetAreaListVO.class);
                zcAssetAreaListVO.setFloorArea_name(zcAssetAreaListVO.getFloorArea());

                zcAssetAreaListVO.setCasingArea_name(zcAssetAreaListVO.getCasingArea());

                zcAssetAreaListVO.setCommonArea_name(zcAssetAreaListVO.getCommonArea());

                zcAssetAreaListVO.setLeasableArea_name(zcAssetAreaListVO.getLeasableArea());

                zcAssetInfoVO.setZc_asset_area(zcAssetAreaListVO);
            }
            ZcAssetImageEntity zcAssetImageEntity = zcAssetInfoService.getZcAssetImage(zcAssetInfoVO.getId());
            if (ObjectUtil.isNotEmpty(zcAssetImageEntity)) {
                ZcAssetImageListVO zcAssetImageListVO = JsonUtil.getJsonToBean(zcAssetImageEntity, ZcAssetImageListVO.class);
                zcAssetImageListVO.setImageDate_name(zcAssetImageListVO.getImageDate());

                zcAssetImageListVO.setImageUrl_name(generaterSwapUtil.getFileNameInJson(zcAssetImageListVO.getImageUrl()));

                zcAssetInfoVO.setZc_asset_image(zcAssetImageListVO);
            }
            //子表数据转换
        }

        for (ZcAssetInfoListVO vo : listVO) {
            FlowTaskEntity flowTaskEntity = generaterSwapUtil.getInfoSubmit(vo.getId(), FlowTaskEntity::getStatus);
            if (flowTaskEntity != null) {
                vo.setFlowState(flowTaskEntity.getStatus());
            } else {
                vo.setFlowState(null);
            }
        }
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zcAssetInfoPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param crData
     * @return
     */
    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody Map<String, Object> crData) throws DataException {
        ZcAssetInfoForm zcAssetInfoForm = JsonUtil.getJsonToBean(crData, ZcAssetInfoForm.class);
        boolean b = zcAssetInfoService.checkForm(zcAssetInfoForm, 0);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        String visualId = "610946161297064773";
        VisualdevEntity visualdevEntity = generaterSwapUtil.getVisualEntity(visualId);
        String mainId = RandomUtil.uuId();

        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        zcAssetInfoForm.setAssetCode(generaterSwapUtil.getBillNumber("ZCBM", false));
        ZcAssetInfoEntity entity = JsonUtil.getJsonToBean(zcAssetInfoForm, ZcAssetInfoEntity.class);
        entity.setId(mainId);
        entity.setCreatorUserId(userProvider.get().getUserId());

        //初始状态
        entity.setStatus("10");
        zcAssetInfoService.save(entity);

        //子表数据
        if (ObjectUtil.isNotEmpty(zcAssetInfoForm.getZc_asset_warrant())) {
            ZcAssetWarrantEntity zc_asset_warrantentity = JsonUtil.getJsonToBean(zcAssetInfoForm.getZc_asset_warrant(), ZcAssetWarrantEntity.class);

            //自动生成的字段
            zc_asset_warrantentity.setAssetId(entity.getId());
            zc_asset_warrantentity.setId(mainId);
            zc_asset_warrantentity.setCreatorUserId(userProvider.get().getUserId());
            zcAssetWarrantService.save(zc_asset_warrantentity);
        }
        if (ObjectUtil.isNotEmpty(zcAssetInfoForm.getZc_asset_area())) {
            ZcAssetAreaEntity zc_asset_areaentity = JsonUtil.getJsonToBean(zcAssetInfoForm.getZc_asset_area(), ZcAssetAreaEntity.class);

            //自动生成的字段
            zc_asset_areaentity.setAssetId(entity.getId());
            zc_asset_areaentity.setId(mainId);
            zc_asset_areaentity.setCreatorUserId(userProvider.get().getUserId());
            zcAssetAreaService.save(zc_asset_areaentity);
        }
//        if (ObjectUtil.isNotEmpty(zcAssetInfoForm.getZc_asset_image())) {
//            ZcAssetImageEntity zc_asset_imageentity = JsonUtil.getJsonToBean(zcAssetInfoForm.getZc_asset_image(), ZcAssetImageEntity.class);
//
//            //自动生成的字段
//            zc_asset_imageentity.setAssetId(entity.getId());
//            zc_asset_imageentity.setId(mainId);
//            zcAssetImageService.save(zc_asset_imageentity);
//        }
        if (FlowStatusEnum.submit.getMessage().equals(zcAssetInfoForm.getStatus())) {
            try {
                generaterSwapUtil.submitFlowTask(visualdevEntity, mainId, zcAssetInfoForm, userInfo, zcAssetInfoForm.getCandidateList(), zcAssetInfoForm.getFlowUrgent());
            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
            return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
    }

    /**
     * 模板下载
     *
     * @return
     */
    @ApiOperation("模板下载")
    @GetMapping("/templateDownload")
    public ActionResult<DownloadVO> TemplateDownload() {
        UserInfo userInfo = userProvider.get();
        DownloadVO vo = DownloadVO.builder().build();
        try {
            vo.setName("职员信息.xlsx");
            vo.setUrl(UploaderUtil.uploaderFile("/api/Common/DownloadModel?encryption=", userInfo.getId() + "#" + "职员信息.xlsx" + "#" + "Temporary"));
        } catch (Exception e) {
            log.error("信息导出Excel错误:{}", e.getMessage());
        }
        return ActionResult.success(vo);
    }

    /**
     * 导出Excel
     *
     * @return
     */
    @ApiOperation("导出Excel")
    @PostMapping("/Actions/Export")
    public ActionResult Export(@RequestBody ZcAssetInfoPagination zcAssetInfoPagination) throws IOException {
        if (StringUtil.isEmpty(zcAssetInfoPagination.getSelectKey())) {
            return ActionResult.fail("请选择导出字段");
        }
        List<ZcAssetInfoEntity> list = zcAssetInfoService.getTypeList(zcAssetInfoPagination, zcAssetInfoPagination.getDataType());
        //处理id字段转名称，若无需转或者为空可删除
        List<ZcAssetInfoListVO> listVO = JsonUtil.getJsonToList(list, ZcAssetInfoListVO.class);
        for (ZcAssetInfoListVO zcAssetInfoVO : listVO) {
            zcAssetInfoVO.setAssetCode_name(zcAssetInfoVO.getAssetCode());

            zcAssetInfoVO.setAssetName_name(zcAssetInfoVO.getAssetName());

            zcAssetInfoVO.setAssetTags_name(zcAssetInfoVO.getAssetTags());

            zcAssetInfoVO.setDetailedAddress_name(zcAssetInfoVO.getDetailedAddress());

            zcAssetInfoVO.setAssetNature_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getAssetNature(), "610823452110094661"));

            zcAssetInfoVO.setAssetUse_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getAssetUse(), "610823865056100677"));

            zcAssetInfoVO.setAssetSource_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getAssetSource(), "610823965094445381"));

            zcAssetInfoVO.setPropertyRightUnit_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getPropertyRightUnit(), "610824055188095301"));

            zcAssetInfoVO.setManagementUnit_name(generaterSwapUtil.getDicName(zcAssetInfoVO.getManagementUnit(), "610824142995849541"));

            zcAssetInfoVO.setManagerId_name(generaterSwapUtil.userSelectValues(zcAssetInfoVO.getManagerId()));

            zcAssetInfoVO.setCoverPictureUrl_name(generaterSwapUtil.getFileNameInJson(zcAssetInfoVO.getCoverPictureUrl()));

            zcAssetInfoVO.setRemark_name(zcAssetInfoVO.getRemark());

            zcAssetInfoVO.setAttachmentUrls_name(generaterSwapUtil.getFileNameInJson(zcAssetInfoVO.getAttachmentUrls()));

            ZcAssetWarrantEntity zcAssetWarrantEntity = zcAssetInfoService.getZcAssetWarrant(zcAssetInfoVO.getId());
            if (ObjectUtil.isNotEmpty(zcAssetWarrantEntity)) {
                ZcAssetWarrantListVO zcAssetWarrantListVO = JsonUtil.getJsonToBean(zcAssetWarrantEntity, ZcAssetWarrantListVO.class);
                zcAssetWarrantListVO.setType_name(generaterSwapUtil.getDicName(zcAssetWarrantListVO.getType(), "610826202277478725"));

                zcAssetWarrantListVO.setCode_name(zcAssetWarrantListVO.getCode());

                zcAssetWarrantListVO.setLandUse_name(zcAssetWarrantListVO.getLandUse());

                zcAssetWarrantListVO.setRoomUse_name(zcAssetWarrantListVO.getRoomUse());

                zcAssetWarrantListVO.setPropertyUse_name(zcAssetWarrantListVO.getPropertyUse());

                zcAssetWarrantListVO.setCertifiedAddress_name(zcAssetWarrantListVO.getCertifiedAddress());

                zcAssetWarrantListVO.setNowPropertyUse_name(zcAssetWarrantListVO.getNowPropertyUse());

                zcAssetWarrantListVO.setLandAuthorityNature_name(generaterSwapUtil.getDicName(zcAssetWarrantListVO.getLandAuthorityNature(), "610826295164535109"));

                zcAssetWarrantListVO.setNumberOblocks_name(zcAssetWarrantListVO.getNumberOblocks());

                zcAssetWarrantListVO.setGeneralFloor_name(zcAssetWarrantListVO.getGeneralFloor());

                zcAssetWarrantListVO.setFloorNum_name(zcAssetWarrantListVO.getFloorNum());

                zcAssetWarrantListVO.setStartDate_name(zcAssetWarrantListVO.getStartDate());

                zcAssetWarrantListVO.setEndDate_name(zcAssetWarrantListVO.getEndDate());

                zcAssetWarrantListVO.setIssueDate_name(zcAssetWarrantListVO.getIssueDate());

                zcAssetWarrantListVO.setCustodyPlace_name(zcAssetWarrantListVO.getCustodyPlace());

                zcAssetWarrantListVO.setRemark_name(zcAssetWarrantListVO.getRemark());

                zcAssetWarrantListVO.setAttachmentUrls_name(generaterSwapUtil.getFileNameInJson(zcAssetWarrantListVO.getAttachmentUrls()));

                zcAssetWarrantListVO.setImageUrl_name(generaterSwapUtil.getFileNameInJson(zcAssetWarrantListVO.getImageUrl()));

                zcAssetInfoVO.setZc_asset_warrant(zcAssetWarrantListVO);
            }
            ZcAssetAreaEntity zcAssetAreaEntity = zcAssetInfoService.getZcAssetArea(zcAssetInfoVO.getId());
            if (ObjectUtil.isNotEmpty(zcAssetAreaEntity)) {
                ZcAssetAreaListVO zcAssetAreaListVO = JsonUtil.getJsonToBean(zcAssetAreaEntity, ZcAssetAreaListVO.class);
                zcAssetAreaListVO.setFloorArea_name(zcAssetAreaListVO.getFloorArea());

                zcAssetAreaListVO.setCasingArea_name(zcAssetAreaListVO.getCasingArea());

                zcAssetAreaListVO.setCommonArea_name(zcAssetAreaListVO.getCommonArea());

                zcAssetAreaListVO.setLeasableArea_name(zcAssetAreaListVO.getLeasableArea());

                zcAssetInfoVO.setZc_asset_area(zcAssetAreaListVO);
            }
            ZcAssetImageEntity zcAssetImageEntity = zcAssetInfoService.getZcAssetImage(zcAssetInfoVO.getId());
            if (ObjectUtil.isNotEmpty(zcAssetImageEntity)) {
                ZcAssetImageListVO zcAssetImageListVO = JsonUtil.getJsonToBean(zcAssetImageEntity, ZcAssetImageListVO.class);
                zcAssetImageListVO.setImageDate_name(zcAssetImageListVO.getImageDate());

                zcAssetImageListVO.setImageUrl_name(generaterSwapUtil.getFileNameInJson(zcAssetImageListVO.getImageUrl()));

                zcAssetInfoVO.setZc_asset_image(zcAssetImageListVO);
            }
            //子表数据转换
        }

        //转换为map输出
        List<Map<String, Object>> mapList = JsonUtil.getJsonToListMap(JsonUtil.getObjectToStringDateFormat(listVO, "yyyy-MM-dd HH:mm:ss"));
        mapList.stream().forEach(map -> {
            Map<String, Object> map2 = new HashMap<>();
            Map<String, Object> map1 = JsonUtil.entityToMap(map.get("zcassetwarrant"));
            for (Map.Entry entry : map1.entrySet()) {
                String key = "zcassetwarrant." + entry.getKey();
                map2.put(key, entry.getValue());
            }
            Map<String, Object> map3 = JsonUtil.entityToMap(map.get("zcassetarea"));
            for (Map.Entry entry : map3.entrySet()) {
                String key = "zcassetarea." + entry.getKey();
                map2.put(key, entry.getValue());
            }
            Map<String, Object> map4 = JsonUtil.entityToMap(map.get("zcassetimage"));
            for (Map.Entry entry : map4.entrySet()) {
                String key = "zcassetimage." + entry.getKey();
                map2.put(key, entry.getValue());
            }
            map.putAll(map2);
        });
        String[] keys = !StringUtil.isEmpty(zcAssetInfoPagination.getSelectKey()) ? zcAssetInfoPagination.getSelectKey().split(",") : new String[0];
        UserInfo userInfo = userProvider.get();
        DownloadVO vo = this.creatModelExcel(configValueUtil.getTemporaryFilePath(), mapList, keys, userInfo);
        return ActionResult.success(vo);
    }

    //导出表格
    public DownloadVO creatModelExcel(String path, List<Map<String, Object>> list, String[] keys, UserInfo userInfo) {
        DownloadVO vo = DownloadVO.builder().build();
        List<ExcelExportEntity> entitys = new ArrayList<>();
        if (keys.length > 0) {
            for (String key : keys) {
                switch (key) {
                    case "jnpf_zc_asset_warrant_jnpf_type":
                        entitys.add(new ExcelExportEntity("产权性质", "zc_asset_warrant.type_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_code":
                        entitys.add(new ExcelExportEntity("权证号", "zc_asset_warrant.code_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_landUse":
                        entitys.add(new ExcelExportEntity("证载土地用途", "zc_asset_warrant.landUse_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_roomUse":
                        entitys.add(new ExcelExportEntity("证载房屋用途", "zc_asset_warrant.roomUse_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_propertyUse":
                        entitys.add(new ExcelExportEntity("证载产权人", "zc_asset_warrant.propertyUse_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_certifiedAddress":
                        entitys.add(new ExcelExportEntity("证载地址", "zc_asset_warrant.certifiedAddress_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_nowPropertyUse":
                        entitys.add(new ExcelExportEntity("现状产权人", "zc_asset_warrant.nowPropertyUse_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_landAuthorityNature":
                        entitys.add(new ExcelExportEntity("土地使用权性质", "zc_asset_warrant.landAuthorityNature_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_numberOblocks":
                        entitys.add(new ExcelExportEntity("幢数", "zc_asset_warrant.numberOblocks_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_generalFloor":
                        entitys.add(new ExcelExportEntity("总楼层", "zc_asset_warrant.generalFloor_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_floorNum":
                        entitys.add(new ExcelExportEntity("所在楼层", "zc_asset_warrant.floorNum_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_startDate":
                        entitys.add(new ExcelExportEntity("开始使用日期", "zc_asset_warrant.startDate_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_endDate":
                        entitys.add(new ExcelExportEntity("使用到期日期", "zc_asset_warrant.endDate_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_issueDate":
                        entitys.add(new ExcelExportEntity("发证日期", "zc_asset_warrant.issueDate_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_custodyPlace":
                        entitys.add(new ExcelExportEntity("产权证保管地", "zc_asset_warrant.custodyPlace_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_remark":
                        entitys.add(new ExcelExportEntity("备注", "zc_asset_warrant.remark_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_attachmentUrls":
                        entitys.add(new ExcelExportEntity("产权证附件", "zc_asset_warrant.attachmentUrls_name"));
                        break;
                    case "jnpf_zc_asset_warrant_jnpf_imageUrl":
                        entitys.add(new ExcelExportEntity("产权证图片", "zc_asset_warrant.imageUrl_name"));
                        break;
                    case "jnpf_zc_asset_area_jnpf_floorArea":
                        entitys.add(new ExcelExportEntity("建筑面积", "zc_asset_area.floorArea_name"));
                        break;
                    case "jnpf_zc_asset_area_jnpf_casingArea":
                        entitys.add(new ExcelExportEntity("套内面积", "zc_asset_area.casingArea_name"));
                        break;
                    case "jnpf_zc_asset_area_jnpf_commonArea":
                        entitys.add(new ExcelExportEntity("公摊面积", "zc_asset_area.commonArea_name"));
                        break;
                    case "jnpf_zc_asset_area_jnpf_leasableArea":
                        entitys.add(new ExcelExportEntity("可租面积", "zc_asset_area.leasableArea_name"));
                        break;
                    case "jnpf_zc_asset_image_jnpf_imageDate":
                        entitys.add(new ExcelExportEntity("近照日期", "zc_asset_image.imageDate_name"));
                        break;
                    case "jnpf_zc_asset_image_jnpf_imageUrl":
                        entitys.add(new ExcelExportEntity("资产近照", "zc_asset_image.imageUrl_name"));
                        break;
                    case "assetCode":
                        entitys.add(new ExcelExportEntity("资产编码", "assetCode_name"));
                        break;
                    case "assetName":
                        entitys.add(new ExcelExportEntity("资产名称", "assetName_name"));
                        break;
                    case "assetTags":
                        entitys.add(new ExcelExportEntity("资产标签", "assetTags_name"));
                        break;
                    case "detailedAddress":
                        entitys.add(new ExcelExportEntity("详细地址", "detailedAddress_name"));
                        break;
                    case "assetNature":
                        entitys.add(new ExcelExportEntity("资产性质", "assetNature_name"));
                        break;
                    case "assetUse":
                        entitys.add(new ExcelExportEntity("资产用途", "assetUse_name"));
                        break;
                    case "assetSource":
                        entitys.add(new ExcelExportEntity("资产来源", "assetSource_name"));
                        break;
                    case "propertyRightUnit":
                        entitys.add(new ExcelExportEntity("权属单位", "propertyRightUnit_name"));
                        break;
                    case "managementUnit":
                        entitys.add(new ExcelExportEntity("管理单位", "managementUnit_name"));
                        break;
                    case "managerId":
                        entitys.add(new ExcelExportEntity("资产管理员", "managerId_name"));
                        break;
                    case "coverPictureUrl":
                        entitys.add(new ExcelExportEntity("资产封面上传", "coverPictureUrl_name"));
                        break;
                    case "remark":
                        entitys.add(new ExcelExportEntity("备注", "remark_name"));
                        break;
                    case "attachmentUrls":
                        entitys.add(new ExcelExportEntity("附件", "attachmentUrls_name"));
                        break;
                    default:
                        break;

                }
            }
            List<String> keylist = new ArrayList<>();
            for (String key : keys) {
                keylist.add(key);
            }
        }

        ExportParams exportParams = new ExportParams(null, "表单信息");
        exportParams.setType(ExcelType.XSSF);
        try {
            @Cleanup Workbook workbook = new HSSFWorkbook();
            if (entitys.size() > 0) {
                if (list.size() == 0) {
                    list.add(new HashMap<>());
                }
                workbook = ExcelExportUtil.exportExcel(exportParams, entitys, list);
            }
            String fileName = "表单信息" + DateUtil.dateNow("yyyyMMdd") + "_" + RandomUtil.uuId() + ".xlsx";

            String temporaryFilePath = configValueUtil.getTemporaryFilePath();
            MultipartFile multipartFile = ExcelUtil.workbookToCommonsMultipartFile(workbook, fileName);
            FileInfo fileInfo = FileUploadUtils.uploadFile(multipartFile, temporaryFilePath, fileName);
            vo.setName(fileInfo.getFilename());
            vo.setUrl(UploaderUtil.uploaderFile(fileInfo.getFilename() + "#" + "Temporary") + "&name=" + fileName);
        } catch (Exception e) {
            log.error("信息导出Excel错误:{}", e.getMessage());
            e.printStackTrace();
        }
        return vo;
    }

    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("信息")
    @GetMapping("/{id}")
    public ActionResult<ZcAssetInfoInfoVO> info(@PathVariable("id") String id) {
        ZcAssetInfoEntity entity = zcAssetInfoService.getInfo(id);
        ZcAssetInfoInfoVO vo = JsonUtil.getJsonToBean(entity, ZcAssetInfoInfoVO.class);

        //子表
        //副表
        QueryWrapper<ZcAssetWarrantEntity> queryWrapperZcAssetWarrant = new QueryWrapper<>();
        queryWrapperZcAssetWarrant.lambda().eq(ZcAssetWarrantEntity::getAssetId, entity.getId());
        ZcAssetWarrantEntity zc_asset_warrantEntity = zcAssetWarrantService.getOne(queryWrapperZcAssetWarrant);
        ZcAssetWarrantModel zcAssetWarrantModel = JsonUtil.getJsonToBean(zc_asset_warrantEntity, ZcAssetWarrantModel.class);
        vo.setZc_asset_warrant(zcAssetWarrantModel);
        QueryWrapper<ZcAssetAreaEntity> queryWrapperZcAssetArea = new QueryWrapper<>();
        queryWrapperZcAssetArea.lambda().eq(ZcAssetAreaEntity::getAssetId, entity.getId());
        ZcAssetAreaEntity zc_asset_areaEntity = zcAssetAreaService.getOne(queryWrapperZcAssetArea);
        ZcAssetAreaModel zcAssetAreaModel = JsonUtil.getJsonToBean(zc_asset_areaEntity, ZcAssetAreaModel.class);
        vo.setZc_asset_area(zcAssetAreaModel);
        QueryWrapper<ZcAssetImageEntity> queryWrapperZcAssetImage = new QueryWrapper<>();
        queryWrapperZcAssetImage.lambda().eq(ZcAssetImageEntity::getAssetId, entity.getId());
        ZcAssetImageEntity zc_asset_imageEntity = zcAssetImageService.getOne(queryWrapperZcAssetImage);
        ZcAssetImageModel zcAssetImageModel = JsonUtil.getJsonToBean(zc_asset_imageEntity, ZcAssetImageModel.class);
        vo.setZc_asset_image(zcAssetImageModel);
        return ActionResult.success(vo);
    }

    /**
     * 更新
     *
     * @param id
     * @param upData
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid Map<String, Object> upData) throws DataException {
        ZcAssetInfoForm zcAssetInfoForm = JsonUtil.getJsonToBean(upData, ZcAssetInfoForm.class);
        boolean b = zcAssetInfoService.checkForm(zcAssetInfoForm, 1);
        if (b) {
            return ActionResult.fail("单行输入不能重复");
        }
        String visualId = "610946161297064773";
        VisualdevEntity visualdevEntity = generaterSwapUtil.getVisualEntity(visualId);
        UserInfo userInfo = userProvider.get();
        ZcAssetInfoEntity entity = zcAssetInfoService.getInfo(id);
        if (entity != null) {
            ZcAssetInfoEntity subentity = JsonUtil.getJsonToBean(zcAssetInfoForm, ZcAssetInfoEntity.class);
            entity.setLastModifyUserId(userProvider.get().getUserId());
            boolean b1 = zcAssetInfoService.updateById(subentity);
            if (!b1) {
                return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
            }
            //子表数据
            if (ObjectUtil.isNotEmpty(zcAssetInfoForm.getZc_asset_warrant())) {
                QueryWrapper<ZcAssetWarrantEntity> queryWrapperZcAssetWarrant = new QueryWrapper<>();
                queryWrapperZcAssetWarrant.lambda().eq(ZcAssetWarrantEntity::getAssetId, entity.getId());
                ZcAssetWarrantEntity zc_asset_warrantOneEntity = zcAssetWarrantService.getOne(queryWrapperZcAssetWarrant);
                ZcAssetWarrantEntity zc_asset_warrantentity = JsonUtil.getJsonToBean(zcAssetInfoForm.getZc_asset_warrant(), ZcAssetWarrantEntity.class);
                zc_asset_warrantentity.setId(zc_asset_warrantOneEntity.getId());
                zc_asset_warrantentity.setAssetId(entity.getId());
                zc_asset_warrantentity.setLastModifyUserId(userProvider.get().getUserId());
                zcAssetWarrantService.updateById(zc_asset_warrantentity);
            }
            if (ObjectUtil.isNotEmpty(zcAssetInfoForm.getZc_asset_area())) {
                QueryWrapper<ZcAssetAreaEntity> queryWrapperZcAssetArea = new QueryWrapper<>();
                queryWrapperZcAssetArea.lambda().eq(ZcAssetAreaEntity::getAssetId, entity.getId());
                ZcAssetAreaEntity zc_asset_areaOneEntity = zcAssetAreaService.getOne(queryWrapperZcAssetArea);
                ZcAssetAreaEntity zc_asset_areaentity = JsonUtil.getJsonToBean(zcAssetInfoForm.getZc_asset_area(), ZcAssetAreaEntity.class);
                zc_asset_areaentity.setId(zc_asset_areaOneEntity.getId());
                zc_asset_areaentity.setAssetId(entity.getId());
                zc_asset_areaentity.setLastModifyUserId(userProvider.get().getUserId());
                zcAssetAreaService.updateById(zc_asset_areaentity);
            }
//            if (ObjectUtil.isNotEmpty(zcAssetInfoForm.getZc_asset_image())) {
//                QueryWrapper<ZcAssetImageEntity> queryWrapperZcAssetImage = new QueryWrapper<>();
//                queryWrapperZcAssetImage.lambda().eq(ZcAssetImageEntity::getAssetId, entity.getId());
//                ZcAssetImageEntity zc_asset_imageOneEntity = zcAssetImageService.getOne(queryWrapperZcAssetImage);
//                ZcAssetImageEntity zc_asset_imageentity = JsonUtil.getJsonToBean(zcAssetInfoForm.getZc_asset_image(), ZcAssetImageEntity.class);
//                zc_asset_imageentity.setId(zc_asset_imageOneEntity.getId());
//                zc_asset_imageentity.setAssetId(entity.getId());
//                zcAssetImageService.updateById(zc_asset_imageentity);
//            }
            if (FlowStatusEnum.submit.getMessage().equals(zcAssetInfoForm.getStatus())) {
                try {
                    generaterSwapUtil.submitFlowTask(visualdevEntity, id, subentity, userInfo, zcAssetInfoForm.getCandidateList(), zcAssetInfoForm.getFlowUrgent());
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
                return ActionResult.success("提交成功，请耐心等待");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZcAssetInfoEntity entity = zcAssetInfoService.getInfo(id);
        if (entity != null) {
            FlowTaskEntity taskEntity = generaterSwapUtil.getInfoSubmit(id, FlowTaskEntity::getId, FlowTaskEntity::getStatus);
            if (taskEntity != null) {
                try {
                    generaterSwapUtil.deleteFlowTask(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zcAssetInfoService.delete(entity);
            //子表数据
            QueryWrapper<ZcAssetWarrantEntity> queryWrapperZcAssetWarrant = new QueryWrapper<>();
            queryWrapperZcAssetWarrant.lambda().eq(ZcAssetWarrantEntity::getAssetId, entity.getId());
            zcAssetWarrantService.remove(queryWrapperZcAssetWarrant);
            QueryWrapper<ZcAssetAreaEntity> queryWrapperZcAssetArea = new QueryWrapper<>();
            queryWrapperZcAssetArea.lambda().eq(ZcAssetAreaEntity::getAssetId, entity.getId());
            zcAssetAreaService.remove(queryWrapperZcAssetArea);
            QueryWrapper<ZcAssetImageEntity> queryWrapperZcAssetImage = new QueryWrapper<>();
            queryWrapperZcAssetImage.lambda().eq(ZcAssetImageEntity::getAssetId, entity.getId());
            zcAssetImageService.remove(queryWrapperZcAssetImage);

        }
        return ActionResult.success("删除成功");
    }

    /**
     * 创建
     *
     * @param imageModel
     * @return
     */
    @PostMapping("/addImage")
    @Transactional
    @ApiOperation("添加近照")
    public ActionResult addImage(@RequestBody @Valid ZcAssetImageModel imageModel) throws DataException {
//        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();
        UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        ZcAssetImageEntity entity = JsonUtil.getJsonToBean(imageModel, ZcAssetImageEntity.class);
//        entity.setId(mainId);
        zcAssetImageService.save(entity);
        return ActionResult.success("创建成功");
    }

    /**
     * 获取字典分类
     *
     * @param
     * @return ignore
     */
    @ApiOperation("获取某个字典数据下拉框列表")
    @GetMapping("/Selector")
    public ActionResult<ListVO<DictionaryTypeSelectVO>> selector() {
        List<DictionaryTypeSelectVO> data = zcAssetInfoService.selectSelectorList(new ZcAssetInfoEntity());
        List<DictionaryTypeSelectModel> voListVO = JsonUtil.getJsonToList(data, DictionaryTypeSelectModel.class);
        List<SumTree<DictionaryTypeSelectModel>> sumTrees = TreeDotUtils.convertListToTreeDot(voListVO);
        List<DictionaryTypeSelectVO> list = JsonUtil.getJsonToList(sumTrees, DictionaryTypeSelectVO.class);
        ListVO<DictionaryTypeSelectVO> vo = new ListVO<>();
        vo.setList(list);
        return ActionResult.success(vo);
    }
}
